$(function () {
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    //关闭对话框
    $("#Cancel").click(function () {
        Series.close();
    });
    //提交新建分组
    $("#addEnsure").click(function () {
        Series.addSubmit();
    });
    //提交编辑分组
    $("#editEnsure").click(function () {
        Series.editSubmit();
    });
});

var Series = {
    reqData:{},
    tipTime : 1500,
}

//关闭此对话框
Series.close = function () {
    parent.layer.close(window.parent.Series.layerIndex);
}
//提交新建分组
Series.addSubmit = function () {
    this.clearData();
    this.collectData();
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    $.ajax({
        type:'post',
        contentType:'application/json',
        dataType:'json',
        url:projectName+'/series/addOne',
        data:JSON.stringify(this.reqData),
        success:function (data) {
            if(data.success){
                Series.close();
                Feng.success(data.msg||"添加成功!");
            } else {
                Feng.error(data.msg||"添加失败!");
            }
        }
    })
}
//提交编辑
Series.editSubmit = function () {
    this.clearData();
    this.collectData();
    var pathName=window.document.location.pathname;
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    $.ajax({
        type:'post',
        contentType:'application/json',
        dataType:'json',
        url:projectName+'/series/editOne',
        data:JSON.stringify(this.reqData),
        success:function (data) {
            if(data.success){
                Series.close();
                Feng.success(data.msg||"修改成功!");
            } else {
                Feng.error(data.msg||"修改失败!");
            }
        }
    })
}
//清除数据
Series.clearData = function () {
    this.reqData = {};
}
//收集数据
Series.collectData = function () {
    this.set('seriesName').set('seriesId','id');
}
//收集数据--按照id,第一个参数是input的id，第二个若没有第一个参数必须是实体的属性名，若有则为实体的属性名，即input的id可以随意起名
Series.set = function (key,modelKey,value) {
    if(typeof value == "undefined"){
        if(typeof $("#" + key).val() =="undefined"){
            var str="";
            var ids="";
            $("input[name='"+key+"']:checkbox").each(function(){
                if(true == $(this).is(':checked')){
                    str+=$(this).val()+",";
                }
            });
            if(str){
                if(str.substr(str.length-1)== ','){
                    ids = str.substr(0,str.length-1);
                }
            }else{
                $("input[name='"+key+"']:radio").each(function(){
                    if(true == $(this).is(':checked')){
                        ids=$(this).val()
                    }
                });
            }
            if(typeof modelKey == "undefined"){
                this.reqData[key] = ids;
            }else {
                this.reqData[modelKey] = ids;
            }
        }else{
            if(typeof modelKey == "undefined"){
                this.reqData[key]= $("#" + key).val();
            }else {
                this.reqData[modelKey] = $("#" + key).val();
            }
        }
    }
    return this;
}
